using System;
using NUnit.Framework;

namespace ShadeTree.Binding.Tests
{
    [TestFixture]
    public class CoercionUtilityTester
    {
        #region Setup/Teardown

        [SetUp]
        public void SetUp()
        {
        }

        #endregion

        public DateTime? Date { get; set; }

        [Test]
        public void CoerceMissingDateAsANull()
        {
            Accessor accessor = SingleProperty.Build<CoercionUtilityTester>(x => x.Date);

            Assert.IsNull(CoercionUtility.Coerce(accessor, ""));
        }

        [Test]
        public void CoerceSoapStyleDate()
        {
            Accessor accessor = SingleProperty.Build<CoercionUtilityTester>(x => x.Date);
            var date = (DateTime) CoercionUtility.Coerce(accessor, "2006-02-01");

            Assert.AreEqual(new DateTime(2006, 2, 1), date);
        }

        [Test]
        public void CoerceToPositiveInteger()
        {
            Assert.IsNull(CoercionUtility.CoerceToPositiveInteger(null));
            Assert.IsNull(CoercionUtility.CoerceToPositiveInteger(""));
            Assert.IsNull(CoercionUtility.CoerceToPositiveInteger("asdf4"));
            Assert.IsNull(CoercionUtility.CoerceToPositiveInteger("-4"));
            Assert.IsNull(CoercionUtility.CoerceToPositiveInteger("1.1"));

            Assert.AreEqual(0, CoercionUtility.CoerceToPositiveInteger("0"));
            Assert.AreEqual(1, CoercionUtility.CoerceToPositiveInteger("1"));
            Assert.AreEqual(5, CoercionUtility.CoerceToPositiveInteger("5"));
            Assert.AreEqual(7, CoercionUtility.CoerceToPositiveInteger("7"));
        }
    }
}